<?php
class ArticleManagerController extends CustomControllerAction {
	public function init() {
		parent::init();
		$this->breadcrumbs->addStep('用户账户', $this->getUrl(null, 'account'));
		$this->breadcrumbs->addStep('文章管理', $this->getUrl(null, 'articlemanager'));
		
		$auth = Zend_Auth::getInstance();
		if (!$auth->hasIdentity()) {
			$this->_redirect($this->getUrl('login', 'account'));
		}
		$this->identity = $auth->getIdentity();
	}
	
	public function indexAction() {
		$month = $this->getRequest()->getQuery('month');
        
        if($month !='' && preg_match('/^(\d{4})-(\d{2})$/', $month, $matches)) {
            $y = $matches[1];
            $m = max(1, min(12, $matches[2]));
        } else {
            $y = date('Y');
            $m = date('n');
        }
        $date = array('y' => $y,
                      'm' => $m);
        $from = mktime(0, 0, 0, $m + 1, 1, $y);
        $to = mktime(0, 0, 0, $m, 1, $y) - 1;
        
        $options = array('userid' => $this->identity->userid,
                         'from'   => $from,
                         'to'     => $to,
                         'order'  => 'a.dateline desc'
                         );    

        $articles = DatabaseObject_Article::GetArticles($this->db, $options);
        $totalArticles = DatabaseObject_Article::GetArticleCount($this->db, 
            array('userid' => $this->identity->userid));
        
        $this->breadcrumbs->addStep('文章列表', $this->getUrl('index'));
        
        $this->view->date = $date;
        $this->view->articles = $articles;
        $this->view->totalArticles = $totalArticles;
	}
    public function editAction() {
        $request = $this->getRequest();
        $articleid = (int) $this->getRequest()->getQuery('id');
        
        $fp = new FormProcessor_ArticleEdit($this->db, $this->identity->userid, $articleid);
        
        if ($request->isPost()) {
            if ($fp->process($request)) {
                $url = $this->getUrl('preview'.'?id='.$fp->article->getId());
                $this->_redirect($url);
            }
        }
        
        if ($fp->article->isSaved()) {
            $this->breadcrumbs->addStep('预览：'.$fp->article->subject, 
                 $this->getUrl('preview').'?id='.$fp->article->getId());
            $this->breadcrumbs->addStep('编辑文章');
        } else {
            $this->breadcrumbs->addStep('创建文章');
        }
        $this->view->fp = $fp;
    }
    
    public function previewAction() {
        $request = $this->getRequest();
        $articleid = (int) $this->getRequest()->getQuery('id');
        $article = new DatabaseObject_Article($this->db);
        
        if (!$article->loadForUser($this->identity->userid, $articleid)) {
            $this->_redirect($this->getUrl('index'));
        }
        
        $this->breadcrumbs->addStep('预览：'.$article->subject, 
                 $this->getUrl('preview').'?id='.$article->getId());
        $this->view->article = $article;
    }
    
    public function setstatusAction() {
        $request = $this->getRequest();
        $articleid = (int)$request->getPost('id');
        
        $article = new DatabaseObject_Article($this->db);
        if (!$article->loadForUser($this->identity->userid, $articleid)) {
            $this->_redirect($this->getUrl());
        }
        
        $url = $this->getUrl('preview').'?id='.$article->getId();
        
        if ($request->getPost('edit')) {
            $this->_redirect($this->getUrl('edit').'?id='.$article->getId());
        } elseif ($request->getPost('publish')) {
            $article->sendLive();
            $article->save();
            
            $this->messager->addMessage('文章已发布');
            
        } elseif ($request->getPost('unpublish')) {
            $article->sendBackToDraft();
            $article->save();
            
            $this->messager->addMessage('发布已取消');
            
        } elseif ($request->getPost('delete')) {
            $article->delete();
            $url = $this->getUrl();
            
            $this->messager->addMessage('文章已删除');
        }
        $this->_redirect($url);
    }
}
?>